//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CallabilityPrice.h"
using namespace Cephei::QL::Instruments;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice (Double amount, QL::Instruments::CallabilityPrice::TypeEnum type) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCallabilityPrice = NULL;
#endif
        QuantLib::Real _amount = (QuantLib::Real)ValueHelper::Convert (amount);
        QuantLib::CallabilityPrice::Type _type = (QuantLib::CallabilityPrice::Type)type ;
        _ppCallabilityPrice = new boost::shared_ptr<QuantLib::CallabilityPrice> (new QuantLib::CallabilityPrice ( _amount,  _type ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCallabilityPrice = NULL;
#endif
        _ppCallabilityPrice = new boost::shared_ptr<QuantLib::CallabilityPrice> (new QuantLib::CallabilityPrice ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice (boost::shared_ptr<QuantLib::CallabilityPrice>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCallabilityPrice = NULL;
#endif
	_ppCallabilityPrice = &childNative;
    
}
Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice (QuantLib::CallabilityPrice& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCallabilityPrice = NULL;
#endif
	_ppCallabilityPrice = new boost::shared_ptr<QuantLib::CallabilityPrice> (&childNative);
    
    _CallabilityPriceOwner = owner;
    
}

Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice (CCallabilityPrice^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCallabilityPrice = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCallabilityPrice = new boost::shared_ptr<QuantLib::CallabilityPrice> (copy->GetShared());
        
    }
}
Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCallabilityPrice = NULL;
#endif
	if (!t->IsSubclassOf(CCallabilityPrice::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice (QuantLib::Handle<QuantLib::CallabilityPrice>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCallabilityPrice = &childNative;
	_ppCallabilityPrice = &static_cast<boost::shared_ptr<QuantLib::CallabilityPrice>>(childNative.currentLink());
    
    _CallabilityPriceOwner = owner;
}
Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice (QuantLib::Handle<QuantLib::CallabilityPrice> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCallabilityPrice = &childNative;
	_ppCallabilityPrice = &static_cast<boost::shared_ptr<QuantLib::CallabilityPrice>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CCallabilityPrice::CCallabilityPrice (QuantLib::CallabilityPrice childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCallabilityPrice = NULL;
#endif
	_ppCallabilityPrice = new boost::shared_ptr<QuantLib::CallabilityPrice> (new QuantLib::CallabilityPrice (childNative));
    
}
#endif

Cephei::QL::Instruments::CCallabilityPrice::~CCallabilityPrice ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCallabilityPrice != NULL)
    {
	    delete _ppCallabilityPrice;
        _ppCallabilityPrice = NULL;
    }
}
Cephei::QL::Instruments::CCallabilityPrice::!CCallabilityPrice ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCallabilityPrice != NULL)
    {
	    delete _ppCallabilityPrice;
    }
}
QuantLib::CallabilityPrice& Cephei::QL::Instruments::CCallabilityPrice::GetReference ()
{
    if (_ppCallabilityPrice == NULL) throw gcnew NativeNullException ();
	return **_ppCallabilityPrice;
}
boost::shared_ptr<QuantLib::CallabilityPrice>& Cephei::QL::Instruments::CCallabilityPrice::GetShared ()
{
    if (_ppCallabilityPrice == NULL) throw gcnew NativeNullException ();
	return *_ppCallabilityPrice;
}
QuantLib::CallabilityPrice* Cephei::QL::Instruments::CCallabilityPrice::GetPointer ()
{
    if (_ppCallabilityPrice == NULL) throw gcnew NativeNullException ();
	return &**_ppCallabilityPrice;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CallabilityPrice>& Cephei::QL::Instruments::CCallabilityPrice::GetHandle ()
{
	if (_phCallabilityPrice == NULL)
	{
		_phCallabilityPrice = new Handle<QuantLib::CallabilityPrice> (*_ppCallabilityPrice);
	}
	return *_phCallabilityPrice;
}
#endif
bool Cephei::QL::Instruments::CCallabilityPrice::HasNative () 
{
	return (_ppCallabilityPrice != NULL);
}

Double Cephei::QL::Instruments::CCallabilityPrice::Amount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCallabilityPrice)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Instruments::CallabilityPrice::TypeEnum Cephei::QL::Instruments::CCallabilityPrice::Type::get ()
{
    try
    {
    	QuantLib::CallabilityPrice::Type _rv = (QuantLib::CallabilityPrice::Type)(*_ppCallabilityPrice)->type ( );   
        QL::Instruments::CallabilityPrice::TypeEnum _nrv = (QL::Instruments::CallabilityPrice::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ICallabilityPrice^ Cephei::QL::Instruments::CCallabilityPrice_Factory::Create (Double amount, QL::Instruments::CallabilityPrice::TypeEnum type)
{
    return gcnew CCallabilityPrice ( amount,  type);
}
Cephei::QL::Instruments::ICallabilityPrice^ Cephei::QL::Instruments::CCallabilityPrice_Factory::Create ()
{
    return gcnew CCallabilityPrice ();
}
